package com.mhkj.supervise.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.mhkj.supervise.dao.SuperviseProblemSchemeDao;
import com.mhkj.supervise.entity.SuperviseProblemScheme;
import com.mhkj.supervise.service.SuperviseProblemSchemeService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class SuperviseProblemSchemeServiceImpl extends BaseServiceImpl<SuperviseProblemScheme> implements SuperviseProblemSchemeService {
	@Resource
	private SuperviseProblemSchemeDao superviseProblemSchemeDao;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	public void setSuperviseProblemSchemeDao(SuperviseProblemSchemeDao SuperviseProblemSchemeDao) {
		super.setBaseRepository(SuperviseProblemSchemeDao);
	}

	@Override
	public Pagination<SuperviseProblemScheme> listByPage(SuperviseProblemScheme SuperviseProblemScheme, int pageIndex, int size) {
		Specification<SuperviseProblemScheme> spec = new Specification<SuperviseProblemScheme>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<SuperviseProblemScheme> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<SuperviseProblemScheme> pageT = superviseProblemSchemeDao.findAll(spec,PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<SuperviseProblemScheme> pagination = new Pagination<SuperviseProblemScheme>();
		pagination.setTotal(superviseProblemSchemeDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	@Override
	public List<SuperviseProblemScheme> findAllByProblemId(Long problemId) {
		Specification<SuperviseProblemScheme> spec = new Specification<SuperviseProblemScheme>() {
		 
			/**
			 * 序号
			 */
			private static final long serialVersionUID = -1718586178504361864L;

			@Override
			public Predicate toPredicate(Root<SuperviseProblemScheme> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				
				 Join<Object, Object> superviseProblem = root.join("superviseProblem",JoinType.INNER);
				 lstPredicates.add(criteriaBuilder.equal(superviseProblem.get("id").as(Long.class),problemId));
				
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		 List<SuperviseProblemScheme> findAll = superviseProblemSchemeDao.findAll(spec);
		 return findAll;
	}

}
